cc.Class({
    extends: cc.Component,

    properties: {
        gameview:cc.Node,
        choseview:cc.Node,
        chengyu:cc.Prefab,
        topsprite:cc.Prefab,
        crr:[],
    },

    onLoad () {
        var self = this;
        cc.loader.loadRes("peizhi/chengyuciku",function(error,TestTxt) {
            self.text = TestTxt.text;
            self.node.emit("LoadFinish",{});
        });
        this.node.on("LoadFinish",function() {
            this.Fenpei();
        },this);

        for (let i = 0; i < 9; i++) {
            let cy = cc.instantiate(this.chengyu);
            cy.parent = this.choseview;
        }
        for (let i = 0; i < 81; i++) {
            let fk = cc.instantiate(this.topsprite);
            fk.parent = this.gameview;
            fk.name = "" + i;
        }
        
    },
    Fenpei(){
        let arr = this.text.split("\n");
        let brr = [];
        for (let i = 1; i < arr.length; i++) {
            brr.push(arr[i]);
        }
        for (let i = 0; i < brr.length; i++) {
            let str = brr[i].split("	");        
            this.crr.push(str[1]);    
        }
        let childs = this.choseview.getChildren();
        var self = this;
        for (let i = 0; i < childs.length; i++) {
            let num = Math.floor(Math.random()*this.crr.length);
            let label = childs[i].getChildByName("Label").getComponent(cc.Label);
            label.string = this.crr[num];
            childs[i].on("mousedown",function(event) {
                var node = event.target;
                let cy = node.getChildByName("Label").getComponent(cc.Label).string;
                let zis = cy.split("");
                var cishu = 0;
                let viewchilds = self.gameview.getChildren();
                self.gameview.getComponent(cc.Layout).enabled = false;
                for (let i = 0; i < viewchilds.length; i++) {
                    if (viewchilds[i].getChildByName("Label").getComponent(cc.Label).string == "") {
                        cishu++;
                    }
                }
                if (cishu == 81) {   
                    viewchilds[22].getChildByName("Label").getComponent(cc.Label).string = zis[0];     
                    viewchilds[31].getChildByName("Label").getComponent(cc.Label).string = zis[1];                  
                    viewchilds[40].getChildByName("Label").getComponent(cc.Label).string = zis[2];                  
                    viewchilds[49].getChildByName("Label").getComponent(cc.Label).string = zis[3];                  
                    self.Shan(cy);
                    self.Huan(cy);
                }else{
                    self.Cha(cy);
                    self.Shan(cy);
                    self.Huan(cy);
                }
            });
        }
    },
    Huan(cy){
        let zis = cy.split("");
        var drr = [];
        for (let i = 0; i < this.crr.length; i++) {
            var t0 = new RegExp(zis[0]);
            var t1 = new RegExp(zis[1]);
            var t2 = new RegExp(zis[2]);
            var t3 = new RegExp(zis[3]);
            if (t0.test(this.crr[i])) {
                drr.push(this.crr[i]);
                this.Shan(this.crr[i]);
            }else if(t1.test(this.crr[i])) {
                drr.push(this.crr[i]);
                this.Shan(this.crr[i]);
            }else if (t2.test(this.crr[i])) {
                drr.push(this.crr[i]);
                this.Shan(this.crr[i]);
            }else if (t3.test(this.crr[i])) {
                drr.push(this.crr[i]);
                this.Shan(this.crr[i]);
            }
        }
        let childs =  this.choseview.getChildren();
        for (let i = 0; i < childs.length; i++) {
            let num = Math.floor(Math.random()*drr.length);
            let label = childs[i].getChildByName("Label").getComponent(cc.Label);
            label.string = drr[num];
        }
    },
    Cha(cy){
        var drr = [];
        let childs = this.gameview.getChildren();
        for (let i = 0; i < childs.length; i++) {
            let str = childs[i].getChildByName("Label").getComponent(cc.Label).string;
            if (str != "") {
                drr.push(str);
            }
        }
        let zis = cy.split("");
        for (let i = 0; i < drr.length; i++) {
            var t0 = new RegExp(zis[0]);
            var t1 = new RegExp(zis[1]);
            var t2 = new RegExp(zis[2]);
            var t3 = new RegExp(zis[3]);
            let zi0 = t0.toString();
            let b0 = zi0.split("/");
            let zi1 = t1.toString();
            let b1 = zi1.split("/");
            let zi2 = t2.toString();
            let b2 = zi2.split("/");
            let zi3 = t3.toString();
            let b3 = zi3.split("/");
            if (t0.test(drr[i])) {
                for (let i = 0; i < childs.length; i++) {
                    let wen = childs[i].getChildByName("Label").getComponent(cc.Label).string;
                    if (wen == b0[1]) {
                        let num = Number(childs[i].name);
                        if ((num + 3)%9 < 6) {
                            if (childs[num + 1].getChildByName("Label").getComponent(cc.Label).string == "" 
                            && childs[num + 2].getChildByName("Label").getComponent(cc.Label).string == ""
                            && childs[num + 3].getChildByName("Label").getComponent(cc.Label).string == "") {
                                childs[num + 1].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                childs[num + 2].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                                childs[num + 3].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                            } 
                            if ((num + 3*9) < 80) {
                                if (childs[num + 9].getChildByName("Label").getComponent(cc.Label).string == "" 
                                && childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string == ""
                                && childs[num + 3*9].getChildByName("Label").getComponent(cc.Label).string == "") {
                                    childs[num + 9].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                    childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                                    childs[num + 3*9].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                                }
                            }
                        }
                    }
                }
            }else if (t1.test(drr[i])) {
                for (let i = 0; i < childs.length; i++) {
                    let wen = childs[i].getChildByName("Label").getComponent(cc.Label).string;
                    if (wen == b1[1]) {
                        let num = Number(childs[i].name);
                        if ((num + 2)%9 < 6) {
                            if (childs[num - 1].getChildByName("Label").getComponent(cc.Label).string == "" 
                            && childs[num + 1].getChildByName("Label").getComponent(cc.Label).string == ""
                            && childs[num + 2].getChildByName("Label").getComponent(cc.Label).string == "") {
                                childs[num - 1].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                childs[num + 1].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                                childs[num + 2].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                            }
                            if ((num + 2*9) < 80) {
                                if (childs[num - 9].getChildByName("Label").getComponent(cc.Label).string == "" 
                                && childs[num + 9].getChildByName("Label").getComponent(cc.Label).string == ""
                                && childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string == "") {
                                    childs[num - 9].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                    childs[num + 9].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                                    childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                                }
                            }
                        }
                    }
                }
            }else if (t2.test(drr[i])) {
                for (let i = 0; i < childs.length; i++) {
                    let wen = childs[i].getChildByName("Label").getComponent(cc.Label).string;
                    if (wen == b2[1]) {
                        let num = Number(childs[i].name);
                        if ((num + 1)%9 < 6) {
                            if (childs[num - 2].getChildByName("Label").getComponent(cc.Label).string == "" 
                            && childs[num - 1].getChildByName("Label").getComponent(cc.Label).string == ""
                            && childs[num + 1].getChildByName("Label").getComponent(cc.Label).string == "") {
                                childs[num - 2].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                childs[num - 1].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                childs[num + 1].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                            }
                            if ((num + 9) < 80) {
                                if (childs[num - 2*9].getChildByName("Label").getComponent(cc.Label).string == "" 
                                && childs[num - 9].getChildByName("Label").getComponent(cc.Label).string == ""
                                && childs[num + 9].getChildByName("Label").getComponent(cc.Label).string == "") {
                                    childs[num - 2*9].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                    childs[num - 9].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                    childs[num + 9].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                                }
                            }else if((num + 3*9 < 80)){
                                if (childs[num + 9].getChildByName("Label").getComponent(cc.Label).string == "" 
                                && childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string == ""
                                && childs[num + 3*9].getChildByName("Label").getComponent(cc.Label).string == "") {
                                    childs[num + 9].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                    childs[num + 2*9].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                    childs[num + 3*9].getChildByName("Label").getComponent(cc.Label).string = b3[1];
                                }
                            }
                        }
                    }
                }
            }else if (t3.test(drr[i])) {
                for (let i = 0; i < childs.length; i++) {
                    let wen = childs[i].getChildByName("Label").getComponent(cc.Label).string;
                    if (wen == b3[1]) {
                        let num = Number(childs[i].name);

                        if (num%9 < 6) {
                            if (childs[num - 3].getChildByName("Label").getComponent(cc.Label).string == "" 
                            && childs[num - 2].getChildByName("Label").getComponent(cc.Label).string == ""
                            && childs[num - 1].getChildByName("Label").getComponent(cc.Label).string == "") {
                                childs[num - 3].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                childs[num - 2].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                childs[num - 1].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                            }
                            if (num < 80 && (num - 3*9) > 8) {
                                if (childs[num - 3*9].getChildByName("Label").getComponent(cc.Label).string == "" 
                                && childs[num - 2*9].getChildByName("Label").getComponent(cc.Label).string == ""
                                && childs[num - 9].getChildByName("Label").getComponent(cc.Label).string == "") {
                                    childs[num - 2*9].getChildByName("Label").getComponent(cc.Label).string = b0[1];
                                    childs[num - 9].getChildByName("Label").getComponent(cc.Label).string = b1[1];
                                    childs[num + 9].getChildByName("Label").getComponent(cc.Label).string = b2[1];
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    Shan(cy){
        for (let i = 0; i < this.crr.length; i++) {
            if (this.crr[i] == cy) {
                delete this.crr[i];
            }
        }
    },
    start () {

    },
    // update (dt) {},
});